#! /usr/bin/env python
# coding=utf-8


import os
import threading

import sys
import argparse


# OGS文件中pep 的文件名称

pep_name = 'pep_raw_ali.fa'
tree_name = 'spe_tree'
out1_name = r'rate4site_out1'
out2_name = r'rate4site_out2'


parser = argparse.ArgumentParser(
    description=''' 计算趋同进化 01 
    对所有的 OGS 运行 rate4site 
    用法:
    run_rate4site.py -a /home/good/tools/my_mir_seeker/rate4site -i OGS_for_testing -t 8
    由大天才于2021年7月19日创建于浙江农业大学''')




parser.add_argument('-a',
                help='必须给定，rate4site脚本的路径')



parser.add_argument('-i',
                help='必须给定，OGS的工作目录')

parser.add_argument('-t',
                help='并行核数 默认为8')

args = parser.parse_args()

if not args.i or not args.a:
    parser.print_help()
    sys.exit()




infile = args.i

ratepath = args.a

if not args.t:
	thread = 8

thread = int(args.t)




index = 0



thread_pool = []



def run_func(file_path):

	global index
	global ratepath

	index = index + 1


	# 单条命令
	# rate4site -t spe_tree  -s pep_raw_ali.fa  -o out_rate4site -y out2 -mj

	a = os.system(ratepath + ' -t %s/%s -s %s/%s -o %s/%s -y %s/%s -mj' % (file_path,tree_name,file_path,pep_name,file_path,out1_name,file_path,out2_name))

	if index >= len(thread_pool):
		return None
	else:
		thread_pool[index].start()

	return	None

n = 0

for i in os.listdir(infile):

	file_path = infile+'/'+i

	n += 1

	th = threading.Thread(target=run_func, args=[file_path], name='th_'+str(n))
	thread_pool.append(th)


for i in range(thread):
	if len(thread_pool)>i:
		thread_pool[i].start()

